package com.example.shirotest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.shirotest.entry.Sites;
import com.example.shirotest.mapper.SitesMapper;
import com.example.shirotest.service.SitesService;
import com.example.shirotest.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SitesServiceImpl extends ServiceImpl<SitesMapper, Sites> implements SitesService {

    @Autowired
    private SitesMapper sitesMapper;

    @Override
    public Boolean updateSitesState(Integer id, Integer state) {
        Sites sites = sitesMapper.selectById(id);
        if(null==sites)return false;

        sites.setState(state);
        sitesMapper.updateById(sites);
        return true;
    }

    @Override
    public Boolean addSites(Sites sites) {
        if(sitesMapper.insert(sites)>0)return true;
        return false;
    }

    @Override
    public Boolean isExist(Sites sites){
        QueryWrapper<Sites> wrapper = new QueryWrapper<>();
        wrapper.eq("url",sites.getUrl());
        wrapper.eq("regex",sites.getRegex());
        Sites siteone = sitesMapper.selectOne(wrapper);
        if(siteone==null)return false;
        return true;
    }

    @Override
    public Boolean updatetime(Integer id,Integer count) {
        Sites sites = sitesMapper.selectById(id);
        if(null==sites)return false;

        //记录更新时间更新数量
        sites.setUpdatetime(DateUtil.getCurDateTimeStr());
        sites.setUpdatecount(count);
        //清除异常未更新记录
        sites.setEmptytime(null);
        sites.setEmptycount(0);
        sitesMapper.updateById(sites);
        return true;
    }

    @Override
    public Boolean emptyCount(Integer id,String cause){
        Sites sites = sitesMapper.selectById(id);
        if(null==sites)return false;

        sites.setEmptytime(DateUtil.getCurDateTimeStr());
        sites.setEmptycount(sites.getEmptycount()+1);
        sites.setEmptycause(cause);
        sitesMapper.updateById(sites);
        return true;
    }

}
